package b2.widget.tech
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.camp.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.fort.*;
	import b2.widget.tab.*;
	import b2.widget.warehouse.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	
	public class ObjectSlot extends Sprite
	{
		public var objectItem:ObjectItem;
		public var objectIndex:int;
		public var basicLevel:int;
		public var strengthenLevel:int;
		public var num:int;
		public var tankId:int;
		
		private var numText:TextField;
		private var fetchButton:BasicButton;
		//关联数组(objectIndex为索引，目标campIndex为值)
		private const objectCampLink:Object = {0:2, 5:7, 6:4, 7:6, 8:11};
		
		public function ObjectSlot() 
		{
			//背景框
			var SlotBgr:Class = ResourceHelper.getInstance().getClassByName("ObjectItemBgr");
			var slotBgr:Sprite = new SlotBgr();
			addChild(slotBgr);
		}
		
		public function get comIndex():String
		{
			return objectIndex + "_" + basicLevel + "_" + strengthenLevel + "_" + tankId;
		}
		
		public function get hasObj():Boolean
		{
			return (objectItem != null); 
		}

		public function loadObj(objectIndex:int, basicLevel:int, strengthenLevel:int, location:int, num:int, tankId:int = 0, 
			shownNumFlag:Boolean = false, showButtonFlag:Boolean = false, showItemNumFlag:Boolean = false):void
		{
			removeObj();
			this.num = num;
			this.objectIndex = objectIndex;
			this.basicLevel = basicLevel;
			this.strengthenLevel = strengthenLevel;
			this.tankId = tankId;
			objectItem = new ObjectItem(objectIndex, basicLevel, strengthenLevel, 0, num, location, showItemNumFlag);
			addChild(this.objectItem);
			if (shownNumFlag)
				showNum();
			if  (showButtonFlag)
				showButton();
		}
		
		private function showButton():void
		{
			removeButton();
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr2");
			fetchButton = new BasicButton(ButtonBgr, "立即获得", 12, 65, 25);
			fetchButton.x = 60;
			fetchButton.y = 25;
			addChild(fetchButton);
			fetchButton.addEventListener(MouseEvent.CLICK, onFecthButtonClick);
		}
		
		//立即获取
		private function onFecthButtonClick(event:MouseEvent):void
		{
			if (!(SceneManager.getInstance().currentScene is CampScene))
				SceneManager.getInstance().loadScene(CampScene.getInstance());
			var campIndex:int = objectCampLink[objectIndex];
			if (campIndex > 0)
			{
				var fortIndex:int = Config.campFortLink[campIndex];
				var fortressDialog:FortressDialog = new FortressDialog(fortIndex, campIndex);
				fortressDialog.startGuide();
				WidgetManager.getInstance().popOut([fortressDialog], true, true);
			}
		} 
		
		private function removeButton():void
		{
			if (fetchButton != null && fetchButton.parent != null)
			{
				fetchButton.parent.removeChild(fetchButton);
				fetchButton = null;
			}
		}
		
		private function showNum():void
		{
			removeNum();
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var ownNum:int = me.objPackage.objectList[comIndex];
			numText = new TextField();
			numText.selectable = false;
        	numText.autoSize = TextFieldAutoSize.LEFT;
        	if (ownNum < num)
				numText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFF0000);
			else
				numText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
			numText.text = Math.min(ownNum, num) + "/" + num;
			numText.x = 65;
			numText.y = 0;
			addChild(numText);
		}
		
		private function removeNum():void
		{
			if (numText != null && numText.parent != null)
			{
				numText.parent.removeChild(numText);
				numText = null;
			}
		}
		
		public function removeObj():void
		{
			removeNum();
			removeButton();
			if (this.objectItem != null && this.objectItem.parent != null)
			{
				objectItem.parent.removeChild(objectItem);
			}
			this.objectItem = null;
			this.objectIndex = -1;
			this.basicLevel = -1;
			this.strengthenLevel = -1;
			this.tankId = -1;
		}
	}
}